package router

import (
	"bluebell/controller"
	_ "bluebell/docs" //这里将生成的swagger文档引入
	"bluebell/logger"
	"bluebell/middlewares"
	"bluebell/utils"
	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

func SetupRouter(mode string) (r *gin.Engine) {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode) //gin默认为debug模式，这里是设置为release模式
	}
	r = gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true)) //这里需要添加使用zap作为日志记录工具的中间件
	v1 := r.Group("/api/v1")
	{
		v1.POST("/signup", controller.SignUpHandler)
		v1.POST("/login", controller.LoginHandler)
		v1.POST("/ping", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "ok3",
			})
		})
	}
	v1.Use(middlewares.JWTAuthMiddleware()) //下面的接口需要校验token了
	{
		v1.GET("/community", controller.CommunityHandler)
	}

	//swagger文档
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

	//匹配不到路由
	r.NoRoute(func(c *gin.Context) {
		utils.ResponseError(c, utils.CodeRouteNotFound)
	})

	return
}
